using System;
using System.Windows.Input;
using System.ComponentModel;

namespace WpfLob.Frame
{
    public class BackCommand : ICommand
    {
        private Presenter _presenter;

        public BackCommand(Presenter presenter)
        {
            _presenter = presenter;

            _presenter.PropertyChanged += PresenterPropertyChanged;
        }

        void PresenterPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentPaneIndex" && CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return _presenter.CurrentPaneIndex > 0;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _presenter.UnbindPaneTitle();
            _presenter.CurrentPaneIndex--;
            _presenter.BindPaneTitle();
        }

        #endregion
    }
}
